package com.lzxzjy.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzxzjy.pojo.Reader;
import com.lzxzjy.serivce.AdminService;
import com.lzxzjy.serivce.ReaderService;
import com.lzxzjy.serivce.impl.AdminServiceImpl;
import com.lzxzjy.serivce.impl.ReaderServiceImpl;
import com.lzxzjy.util.CheckCodeUtil;
import com.lzxzjy.util.EncryptionUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

@WebServlet("/login/*")
public class LoginServlet extends BaseServlet {

    private final AdminService adminService = new AdminServiceImpl();
    private final ReaderService readerService = new ReaderServiceImpl();

    // 获取验证码
    public void getCode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        ServletOutputStream outputStream = response.getOutputStream();
        String checkCode = CheckCodeUtil.outputVerifyImage(170, 40, outputStream, 6);
        session.setAttribute("checkCodeGen", checkCode);
    }

    // 校验验证码
    public void checkCode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String captcha = request.getParameter("captcha");
        HttpSession session = request.getSession();
        String checkCodeGen = (String) session.getAttribute("checkCodeGen");
        if (!checkCodeGen.equalsIgnoreCase(captcha)) {
            response.getWriter().write("failure");
        }
    }

    // 读者登录
    public void CheckReaderPassword(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        BufferedReader readerReader = request.getReader();
        String readerLoginLine = readerReader.readLine();
        JSONObject readerLogin = JSON.parseObject(readerLoginLine);
        Reader reader = readerService.selectReaderByUsernameAndPassWord((String) readerLogin.get("username"), EncryptionUtil.encrypt((String) readerLogin.get("password")));

        if (reader == null) {
            response.getWriter().write("incorrect");
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("reader", reader);
        }
    }

    //读者登出
    public void logoutReader(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("reader");
    }


    // 管理员登录
    public void CheckAdministratorPassword(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String adminLoginLine = reader.readLine();
        JSONObject adminLogin = JSON.parseObject(adminLoginLine);
        String adminName = adminService.selectAdminNameByUsernameAndPassWord((String) adminLogin.get("username"), EncryptionUtil.encrypt((String) adminLogin.get("password")));
        if (adminName.isEmpty()) {
            response.getWriter().write("incorrect");
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("adminName", adminName);
        }
    }

    public void logoutAdmin(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("adminName");
    }
}
